/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.questionnaire;

import com.blandware.atleap.model.core.BaseObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Answer option for questions where possible answerOptions are known.</p>
 * <p><a href="AnswerOption.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2006/06/25 14:44:39 $
 * @struts.form include-all="false" extends="BaseForm"
 * @hibernate.class table="`al_questionnaire_option`" lazy="false"
 * @hibernate.cache usage="read-write"
 */
public class AnswerOption extends BaseObject {

	/**
	 * ID of this option
	 */
	protected Long id;

	/**
	 * Question to which this option belongs
	 */
	protected Question question;

	/**
	 * Map of titles for different locales
	 */
	protected Map title = new HashMap();

    /**
     * Position
     */
    protected Integer position;

    /**
     * Version of this option (used in optimistic locking)
     */
	protected Long version;

    /**
     * List of answers in which this answer option is selected
     */
    protected List answers = new ArrayList();

	/**
	 * Returns the id of the option.
	 *
	 * @return id
	 * @struts.form-field
	 * @hibernate.id column="`id`"
	 * generator-class="increment" unsaved-value="null"
	 */
	public Long getId() {
		return id;
	}

    /**
     * Sets id of the option.
     *
     * @param id the id to be set
     */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Returns the question.
	 *
     * @return the question
	 * @struts.form-field
	 * @hibernate.many-to-one column="`question_id`" not-null="true"
     * outer-join="false" lazy="false"
	 */
	public Question getQuestion() {
		return question;
	}

    /**
     * Sets the question of this option.
     *
     * @param question question to set
     */
	public void setQuestion(Question question) {
		this.question = question;
	}

	/**
	 * Gets mapping from locale identifiers to titles of this option.
	 *
	 * @return mapping from locale identifiers to titles
	 * @struts.form-field
	 * @hibernate.map table="`al_questionnaire_option_title`" lazy="true"
     * cascade="all" outer-join="false"
	 * @hibernate.collection-key column="`id`"
	 * @hibernate.collection-index column="`locale_identifier`" type="string"
     * length="10"
	 * @hibernate.collection-element type="string" column="`title`"
     * not-null="false" length="252"
	 */
	public Map getTitle() {
		return title;
	}

    /**
     * Sets mapping from locale identifiers to titles of this option.
     *
     * @param title mapping from locale identifiers to titles
     */
	public void setTitle(Map title) {
		this.title = title;
	}

    /**
     * Returns position.
     *
     * @return position
     * @hibernate.property column="`pos`" not-null="true" unique="false"
     */
    public Integer getPosition() {
        return position;
    }

    /**
     * Sets position.
     *
     * @param position position to set
     */
    public void setPosition(Integer position) {
        this.position = position;
    }

	/**
	 * Returns version of the option.
	 *
	 * @return version
	 * @struts.form-field
	 * @hibernate.version column="`version`" type="long" unsaved-value="null"
	 */
	public Long getVersion() {
		return version;
	}

    /**
     * Sets version of the option.
     *
     * @param version the version to set
     */
	public void setVersion(Long version) {
		this.version = version;
	}

    /**
     * Returns a list of answers in which this option is selected.
     *
     * @return list of answers
     * @hibernate.bag table="`al_questionnaire_option_answer`" cascade="none"
     * lazy="true" inverse="false" outer-join="false"
     * @hibernate.collection-key
     * @hibernate.collection-key-column name="`option_id`"
     * @hibernate.collection-many-to-many class="com.blandware.atleap.model.questionnaire.Answer"
     * column="`answer_id`" outer-join="false"
     */
    public List getAnswers() {
        return answers;
    }

    /**
     * Sets a list of answers in which this option is selected.
     *
     * @param answers list to set
     */
    public void setAnswers(List answers) {
        this.answers = answers;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof AnswerOption)) {
            return false;
        }

        final AnswerOption answerOption = (AnswerOption) o;

        if (question != null
                ? !question.equals(answerOption.question)
                : answerOption.question != null) {
            return false;
        }
        if (title != null
                ? !title.equals(answerOption.title) : answerOption.title != null) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = (question != null ? question.hashCode() : 0);
        result = 29 * result + (title != null ? title.hashCode() : 0);
        return result;
    }
}
